class ByteUtils {

  constructor() {
    if (!ByteUtils.instance) {
      ByteUtils.instance = this;
    }
    return ByteUtils.instance;
  }

  /**
   * ArrayBuffer转16进制字符串事例
   */
  ab2hex(buffer, interval) {
    var hexArr = Array.prototype.map.call(
      new Uint8Array(buffer),
      function (bit) {
        return ('00' + bit.toString(16)).slice(-2)
      }
    )
    return hexArr.join(interval ? interval : "").toLocaleUpperCase();
  }

  /**
   * ArrayBuffer转Array
   */
  ab2Array(buffer) {
    let dataView = new DataView(buffer)
    let bytes = new Array(buffer.byteLength)
    for (let i = 0; i < bytes.length; ++i) {
      bytes[i] = dataView.getUint8(i)
    }
    return bytes
  }

  /**
   * 获取随机数组
   * @param {长度} size 
   * @param {随机值的最大值} maxValue 
   */
  getRandomBytes(size, maxValue) {
    let randomBytes = new Array();
    for (let i = 0; i < size; ++i) {
      randomBytes.push(Math.floor(Math.random() * maxValue));
    }
    return randomBytes;
  }

  /**
   * 获取随机的Ascii码字符串
   * @param {字符串长度} length 
   */
  getRandomAsciiStr(length) {
    let resultStr = "";
    for (let i = 0; i < length; ++i) {
      let char = 48 + Math.random() * 10;
      resultStr += String.fromCharCode(char);
    }
    // console.log("RandomAsciiStr = " + resultStr)
    return resultStr;
  }

  /**
   * 字符串转ASCII码数组
   * @param {字符串} str 
   */
  strToAsciiBytes(str) {
    let bytes = new Array();
    for (let i = 0; i < str.length; ++i) {
      let char = str.charCodeAt(i);
      bytes.push(char);
    }
    console.log(bytes)
    return bytes;
  }

  /**
   * 数组转ASCII码字符串
   * @param {数组} bytes 
   */
  bytesToAsciiStr(bytes) {
    // console.log(bytes)
    let str = "";
    for (let i = 0; i < bytes.length; ++i) {
      if (bytes[i] == 0) {
        continue;
      }
      let temp = String.fromCharCode(bytes[i]);
      // console.log(temp)
      str += temp;
    }
    // console.log(str)
    return str;
  }

  /**
   * 将byte数组转化成short
   */
  bytesToShort(bytes) {
    var res = (bytes[0] << 8) & 0xff00;
    res |= (bytes[1] & 0xff)
    return res
  }

  /**
   * 将bytes数组转化成UUID
   */
  bytesToUuid(data) {
    if (data == null || data.length < 16) {
      return null
    }
    // var bMost = data.slice(0, 8)
    // var bLeast = data.slice(8, 16)
    var uuid = this.ab2hex(data.slice(0, 4), "") + '-' + this.ab2hex(data.slice(4, 6), "") + '-' + this.ab2hex(data.slice(6, 8), "") + '-' + this.ab2hex(data.slice(8, 10), "") +
      '-' + this.ab2hex(data.slice(10, 16), "");
    console.log('uuid = ' + uuid)
    return uuid
  }

  /**
   * 将字节数组转化成int
   * @param {数组} b 
   */
  bytesToInt(b) {
    let i = b[0] & 0xff << 24;
    i |= (b[1] & 0xff << 16);
    i |= (b[2] & 0xff << 8);
    i |= (b[3] & 0xff);
    return i;
  }

  /**
   * 随机生成16位UUID
   */
  randomUUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      const r = (Math.random() * 16) | 0,
        v = c === 'x' ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    });
  }

  /**
   * UUID to Bytes
   */
  uuidToBytes(uuid) {
    var str = new String(uuid)
    str = str.replace(/-/g, '')
    // console.log('uuidToBytes：str = ' + str)
    return this.stringToBytes(str)
  }

  /**
   * String to Bytes
   */
  stringToBytes(str) {
    let bytes = new Array()
    for (let i = 0; i < 16; ++i) {
      let temp = str.slice(2 * i, 2 * (i + 1))
      bytes.push(parseInt(temp, 16))
    }
    // console.log(bytes)
    return bytes
  }

    /**
   * String to Bytes
   */
    stringToBytesByLen(str, len) {
      let bytes = new Array()
      for (let i = 0; i < len; ++i) {
        let temp = str.slice(2 * i, 2 * (i + 1))
        bytes.push(parseInt(temp, 16))
      }
      // console.log(bytes)
      return bytes
    }

  /**
   * String to Bytes
   * Dscp 7字节时间转换成Bytes
   */
  dateStringToBytes(str) {
    var bytes = new Array()
    for (var i = 0; i < str.length; ++i) {
      var temp = str.slice(i, i + 1)
      bytes.push(parseInt(temp, 10))
    }
    return bytes
  }

  /**
   * 将Int转化为Byte数组
   *
   * @param i
   * @return
   */
  intToBytes2(i) {
    var res = new Array(2);
    res[0] = ((i >> 8) & 0xFF);
    res[1] = (i & 0xFF);
    return res;
  }

  /**
   * 将Int转化为Byte4数组
   *
   * @param i
   */
  intToBytes4(i) {
    var res = new Array(4);
    res[3] = (i & 0xFF);
    res[2] = (i >> 8 & 0xFF);
    res[1] = (i >> 16 & 0xFF);
    res[0] = (i >> 24 & 0xFF);
    return res;
  }

  /**
   * 将Byte数组转化为int,取第一二个字节
   *
   * @param b
   * @return
   */
  bytes2ToInt(b) {
    var c = ((b[0] << 8) & 0xFF00);
    c |= (b[1] & 0xFF);
    return c;
  }

  /**
   * 获取子byte数组
   */
  subBytes(data, start, len) {
    var res = new Array(len);
    for (var i = 0; i < len; i++) {
      res[i] = data[start + i];
    }
    return res;
  }

  /**
 * 将16进制字符串转换成字节数组
 * @param {string} hexString - 16进制字符串
 * @returns {Uint8Array} - 字节数组
 */
 hexStringToByteArray(hexString) {
  // 计算字节数组的长度
  var byteArrayLength = Math.ceil(hexString.length / 2);
  // 创建字节数组
  var byteArray = new Array(byteArrayLength);
  
  for (var i = 0; i < byteArrayLength; i++) {
      // 每两个字符代表一个字节，转换为整数
      byteArray[i] = parseInt(hexString.substr(i * 2, 2), 16);
  }
  
  return byteArray;
}

}

export {
  ByteUtils
}